﻿angular.module('app')
    .directive('userInfo', function () {
        return {
            restrict: "E",
            replace: true,
            controller: ['$scope','$rootScope','UserSrv','WorkContextSrv',function ($scope, $rootScope, UserSrv, WorkContextSrv) {
                var currentUser = WorkContextSrv.getCurrentUser();
                var followingList = currentUser.following;
                var currentUserList = [];
                //view user info in popup
                $rootScope.$on("openPopupViewUserInfo", function (e, params) {
                    currentUserList = [];
                    var userName = params.userName;
                    currentUserList = params.currentUserList;
                    $scope.onLoadingUser = false;
                    $scope.followText = "Follow";
                    $scope.followed = false;

                    if (!$scope.onLoadingUser) {
                        $scope.onLoadingUser = true;

                        //Open popup
                        UserSrv.getUserByName(userName, true).then(function (dt) {
                            if (dt !== 'true') {
                                //$rootScope.$broadcast("MODAL:SHOW");
                                jQuery('#viewuserinfo').modal('show');
                                //convert birtday format
                                var birtdayFormated = '';
                                $scope.userInfo = dt;
                                if (dt.birthday != null) {
                                    birtdayFormated = dt.birthday.replace(/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/g,
                                                    function ($0, $1, $2, $3, $4, $5, $6) {
                                                        return $2 + "/" + $3 + "/" + $1; //mm/dd/yyyy
                                                    }
                                                );
                                    $scope.userInfo.birthday = birtdayFormated;
                                }

                                $scope.display = dt.userName != currentUser.userName;
                                for (var i = 0; i < followingList.length; i++) {
                                    if (dt.userName == followingList[i].userName) {
                                        $scope.followed = dt.userName == followingList[i].userName;
                                        $scope.followText = "UnFollow";
                                        break;
                                    }
                                }
                                $scope.onLoadingUser = false;
                            }
                        });

                        //jQuery('#viewuserinfo').on('hidden.bs.modal', function() {
                        //    $rootScope.$broadcast("MODAL:HIDE");
                        //});
                    }
                });

                //Follow user
                $scope.follow = function (user) {
                    //show indicator
                    $scope.onSubmitting = true;
                    //unfollow
                    if ($scope.followed) {
                        UserSrv.removeFollowing({ userName: currentUser.userName, followerName: user.userName }).success(function (dt, st) {
                            $scope.followed = false;
                            $scope.followText = "Follow";
                            //remove follow from list
                            for (var i = 0; i < followingList.length; i++) {
                                if (followingList[i].userName == user.userName) {
                                    //remove follower from list
                                    followingList.splice(i, 1);
                                    //Update current list
                                    currentUser.following = followingList;
                                    WorkContextSrv.setCurrentUser(currentUser);
                                    //reload Table
                                    $scope.$broadcast("reloadFollowingTable", currentUserList);
                                    break;
                                }
                            }
                            //console.log(dt);
                            //hide indicator
                            $scope.onSubmitting = false;
                        }).error(function (dt, st) {
                            //console.log(dt);
                            //hide indicator
                            $scope.onSubmitting = false;
                        });
                    } else { //follow
                        UserSrv.followUser({ userName: currentUser.userName, followerName: user.userName }).success(function (dt, st) {
                            $scope.followed = true;
                            $scope.followText = "UnFollow";
                            //add follower to following list
                            followingList.push(user);
                            //Update current list
                            currentUser.following = followingList;
                            WorkContextSrv.setCurrentUser(currentUser);

                            //reload Table
                            $scope.$broadcast("reloadFollowingTable", currentUserList);

                            //console.log(dt);

                            //hide indicator
                            $scope.onSubmitting = false;
                        }).error(function (dt, st) {
                            //console.log(dt);
                            //hide indicator
                            $scope.onSubmitting = false;
                        });
                    }

                };
            }],
            templateUrl: "scripts/app/views/user/info.html"
        };
    })
    .directive("followingCount", ['WorkContextSrv', function (workContextSrv) {
        return {
            restrict: 'E',
            scope: true,
            replace: true,
            templateUrl: 'scripts/app/views/user/following.count.html',
            link: function (scope) {
                var currentUser = workContextSrv.getCurrentUser();
                scope.followingCount = currentUser.following.length;
                scope.$watch(workContextSrv.getCurrentUser().following.length, function () {
                    //get following count
                    scope.followingCount = workContextSrv.getCurrentUser().following.length;
                });
            }
        };
    }])
    .directive("followerCount", ['WorkContextSrv', function (workContextSrv) {
        return {
            restrict: 'E',
            scope: true,
            replace: true,
            templateUrl: 'scripts/app/views/user/follower.count.html',
            link:function (scope) {
                var currentUser = workContextSrv.getCurrentUser();

                //get get follower count
                scope.followerCount = currentUser.followerCount;
            }
        };
    }])
;